﻿using System.Windows;
using Ping.App;

namespace Ping.Client.Controls
{
	public partial class ConnectionControl
	{
		private Connection _connection;

		public ConnectionControl()
		{
			InitializeComponent();
			Loaded += ControlLoaded;
			Unloaded += ControlUnloaded;
		}

		private void ControlLoaded(object sender, RoutedEventArgs e)
		{
			if (StateService.State == null)
				return;

			_connection = StateService.State.Connection;
			_connection.StateChanged += ConnectionStateChanged;

			ConnectionStateChanged();
		}

		private void ConnectionStateChanged()
		{
			/*Dispatcher.BeginInvoke(() =>
			                       	{
			                       		status.Text = string.Format("[App. Server: {0}]", _connection.State);
			                       	});*/
		}

		private void ControlUnloaded(object sender, RoutedEventArgs e)
		{
			_connection.StateChanged -= ConnectionStateChanged;
			_connection = null;
		}
	}
}